﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Steema.TeeChart.Styles;
using System.Drawing.Drawing2D;
using System.Diagnostics;

namespace ZyEddyTeeChart
{
    public partial class ZyEddyImpChart : UserControl
    {
        Bitmap bmp;
        double alarmValue;
        public ZyEddyImpChart()
        {
            InitializeComponent();
            Steema.TeeChart.Texts.ExpireMsg1 = "";
            Steema.TeeChart.Texts.ExpireMsg2 = "";

            alarmValue = 50;
            

        }
       public double AlarmValue {
            get
            {
                return alarmValue;
            }
            set {
                alarmValue=value;
                DrawingAlarm();
                tChart.Refresh();
            }
        }
       public HorizLine Imp1
        {
            get
            {
                return    horizLine1;
            }
        }
        public HorizLine Imp2
        {
            get
            {
                return horizLine2;
            }
        }
        #region
        // Direct draw for update
        //private void drawGrid(Steema.TeeChart.Drawing.Graphics3D g)
        //{
        //    g.Pen.Color = Color.LightGray;
        //    g.Pen.Style = System.Drawing.Drawing2D.DashStyle.Dash;
        //    for (int i = 1; i < 10; i++)
        //    {
        //        if (i != 5)
        //        {
        //            g.Line(new Point(tChart[0].CalcXPosValue(-100), tChart[0].CalcXPosValue(-100 + i * 20)),
        //               new Point(tChart[0].CalcXPosValue(100), tChart[0].CalcXPosValue(-100 + i * 20)));
        //            g.Line(new Point(tChart[0].CalcYPosValue(-100+i*20), tChart[0].CalcYPosValue(-100)),
        //              new Point(tChart[0].CalcYPosValue(-100 + i * 20), tChart[0].CalcYPosValue(100)));
        //        }
        //    }
        //}
        //private void drawAlarmArea(Steema.TeeChart.Drawing.Graphics3D g)
        //{

        //    GraphicsPath gPath = new GraphicsPath();
        //    int x1 =tChart[0].CalcXPosValue(-100);
        //    int y1 = tChart[0].CalcYPosValue(-100);
        //    int x2 = tChart[0].CalcXPosValue(100);
        //    int y2 = tChart[0].CalcYPosValue(100);

        //    gPath.AddRectangle(new Rectangle(x1, y1, x2 - x1, y2 - y1));
        //     x1 = tChart[0].CalcXPosValue(-AlarmValue);
        //     y1 = tChart[0].CalcYPosValue(-AlarmValue);
        //     x2 = tChart[0].CalcXPosValue(AlarmValue);
        //     y2 = tChart[0].CalcYPosValue(AlarmValue);
        //    gPath.AddArc(new Rectangle(x1, y1, x2 - x1, y2 - y1), 0, 360);
        //    Color customColor = Color.FromArgb(185, Color.Red);
        //    SolidBrush brush = new SolidBrush(customColor);
        //    g.FillRegion(brush, new Region(gPath));
        //}
        #endregion
        private void tChart_BeforeDrawSeries(object sender, Steema.TeeChart.Drawing.Graphics3D g)
        {
                 if(bmp!=null)
                    g.Draw(0, 0, bmp);
          }
        public void  DrawingAlarm()
        {
            if (bmp != null)
                bmp.Dispose();
            if (Width == 0 || Height == 0) return;
            bmp = new Bitmap(Width, Height);
            Graphics g = Graphics.FromImage(bmp);
            Pen pen = new Pen(Color.LightGray);
            pen.DashStyle = DashStyle.Dash;
            for (int i = 1; i < 10; i++)
            {
                if (i != 5)
                {
                    g.DrawLine(pen, 0, 0 + Height / 10 * i, Width, 0 + Height / 10 * i);
                    g.DrawLine(pen, Width / 10 * i, 0, Width / 10 * i, Height);
                }
            }

            GraphicsPath gPath = new GraphicsPath();


            gPath.AddRectangle(new Rectangle(0, 0, Width, Height));
            int x1 = Width / 2 - (int)(Width * AlarmValue / 200);
            int w = (int)(Height * AlarmValue / 100);
            gPath.AddArc(new Rectangle(x1, x1, w, w), 0, 360);
            Color customColor = Color.FromArgb(185, Color.Red);
            SolidBrush brush = new SolidBrush(customColor);
            g.FillRegion(brush, new Region(gPath));

            g.Dispose();
        }
    
        private void tChart_SizeChanged(object sender, EventArgs e)
        {
             DrawingAlarm();
        }
    }
}

/*
//1、在内存中建立一块“虚拟画布”
        Bitmap bmp = new Bitmap(200,200);

      //2、获取这块内存画布的Graphics引用
        Graphics bufferGraphics = Graphics.FromImage(bmp);

      //3、在这块内存画布上绘图
        bufferGraphics.Clear(this.BackColor);
      bufferGraphics.DrawRectangle(Pens.Black,0,0,bmp.Width -1,bmp.Height -1);
      bufferGraphics.DrawEllipse(Pens.Red,10,10,100,50);
      bufferGraphics.DrawLine(Pens.Green,10,100,100,200);

      //4、将内存画布画到窗口中
        using(Graphics g = e.Graphics)
        {
            g.DrawImage(bmp, 10, 10);
        }
        
      //5. 释放资源
        bmp.Dispose();
      bufferGraphics.Dispose();	

————————————————

版权声明：本文为CSDN博主「bluecard2008」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。

原文链接：https://blog.csdn.net/bluecard2008/article/details/103758664
2.



     BufferedGraphicsContext currentContext=BufferedGraphicsManager.Current;
        BufferedGraphics myBuffer = currentContext.Allocate(e.Graphics,e.ClipRectangle);

        Graphics g = myBuffer.Graphics;

        g.Clear(this.BackColor);
        g.DrawRectangle(Pens.Black, 10, 10, 200, 200);
        g.DrawEllipse(Pens.Red, 10, 10, 100, 50);
        g.DrawLine(Pens.Green, 10, 100, 100, 200);

        myBuffer.Render(e.Graphics);  //呈现图像至关联的Graphics

        myBuffer.Dispose();
        g.Dispose();

————————————————

版权声明：本文为CSDN博主「bluecard2008」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。

原文链接：https://blog.csdn.net/bluecard2008/article/details/103758664
 */
